package cn.newgrand.netcall.base;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.ContactList;

import cn.newgrand.common.ChineseToPY;
import cn.newgrand.netcall.search.contact.ContactSearch;
import cn.newgrand.netcall.search.contact.ContactSearchBean;
import cn.newgrand.netcall.search.contact.ContactSearchManager;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-8-5 上午11:22:31
 * 类说明
 */
public class ContactManager {
	private static ContactManager singleton;
	private static final Object LOCK = new Object();
	private ContactSearchManager contactSearchManager ;
	private static File transcriptDir = new File(SparkManager.getUserDirectory(), "transcripts");
	private Map<String,ContactSearchBean> contactPYMap = new HashMap<String,ContactSearchBean>();
	private Map<String, ContactBean> cMap;
	
	private ContactManager(){}
	public static ContactManager getInstance(){
		synchronized(LOCK){
		if(null == singleton){
			singleton = new ContactManager(); 
			return singleton;
		}
		else 
			return singleton;
		}
	}
	
	public ContactSearchManager getContactSearchManager(){
		return ContactSearchManager.getInstance();
	}
	
	/**
     * 得到所有的联系人
     * 
     * @return
     */
    public Map<String,ContactBean> getAllContacts(){
    	return SparkManager.getContactList().getAllContacts();
    	 
    }
    /**
     * 如果是本地客户端中联系人则返回 true
     * @param jid
     * @return
     */
    public boolean isLocalContact(String jid){
    	cMap = getAllContacts();
    	if(null!=cMap.get(jid)){
    		return true;
    	}else 
    		return false;
    }
    
    /**
     * 加载所有的联系人
     * 并将联系人中的中文属性转换为拼音
     * 以作查询用
     * 
     * @return
     */
    public Map<String,ContactSearchBean> loadAllContactsPY(){
    	cMap = getAllContacts();
		
		ContactSearchBean csb;
		String JID;
		String namePY;
		String namePYF;
		String nickNamePY;
		String nickNamePYF;
		// Make sure we are using the default glass pane
		for (ContactBean contact : cMap.values()) {
			JID = contact.getJID();
			namePY = ChineseToPY.cn2Spell(contact.getName());
			namePYF = ChineseToPY.cn2FirstSpell(contact.getName());
			nickNamePY = ChineseToPY.cn2Spell(contact.getNickName());
			nickNamePYF = ChineseToPY.cn2FirstSpell(contact.getNickName());
			csb = new ContactSearchBean(contact,namePY,namePYF,nickNamePY,nickNamePYF);
			contactPYMap.put(contact.getJID(),csb);
		}
    	return contactPYMap;
    }
    
    /**
     * 得到所有汉语转拼音后的联系人
     * 
     * @return
     */
    public Map<String,ContactSearchBean> getAllContactsPY(){
    	cMap = getAllContacts();
		if (cMap.size() != contactPYMap.size()) {
			loadAllContactsPY();
		}
    	return contactPYMap;
    }
	
	/**
     * 得到最近的联系人，根据聊天时间进行排序
     * 返回前十五条
     * @return the collection of favorite people (jids)
     */
	public List<String> getRecentContacts() {
        if (!transcriptDir.exists()) {
            return Collections.emptyList();
        }

        final File[] transcriptFiles = transcriptDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return !name.contains("_current") && !name.equals("conversations.xml");
            }
        });
        final List<File> files = Arrays.asList(transcriptFiles);
        
        //最近联系人
        Collections.sort(files,modifyTimeComparator);
        int size = files.size();
        if(size>15){
        	size=15;
        }
        final List<String> jidList = new ArrayList<String>();
        for (int i = 0; i < size; i++) {
            File file = files.get(i);
            String jid;

            final String fileName = file.getName();
            final int dot = fileName.lastIndexOf('.');
            jid = dot > 0 ? fileName.substring(0, dot) : fileName;

            jidList.add(jid);
        }

        return jidList;
    }
	
	
    //文件修改时间由近到远排序
    final Comparator<File> modifyTimeComparator = new Comparator<File>() {
        public int compare(File item1, File item2) {
            long int1 = item1.lastModified();
            long int2 = item2.lastModified();

            if (int1 == int2) {
                return 0;
            }

            if (int1 > int2) {
                return -1;
            }

            if (int1 < int2) {
                return 1;
            }

            return 0;
        }
    };
    
	
}
